package tv_megazineplus
import pt.sapo.services.metadata.epg.EPG
import pt.sapo.services.metadata.epg.EPGSoap
import java.text.SimpleDateFormat

class LoadEAPService {

    static transactional = true

    def serviceMethod() {
        EPGSoap epg = (new EPG()).getEPGSoap()
        def chanels = epg.getChannelList().getChannel()
        
        chanels.each{ c ->
            //System.out.println("\n--------------------------\n");
            //System.out.println("Canal: " + c.getName());
            def channelInstance = new Channel2()
            
            channelInstance.channel_name = c.getName()
            //System.out.println("Sigla: " + c.getSigla());
            channelInstance.channel_sigla = c.getSigla()
            
            if(!Channel2.findWhere(channel_name: c.getName()))
            channelInstance.save()
            else
            channelInstance = Channel2.findWhere(channel_name: c.getName())
            
            //Cria objecto com duas horas de avanço
            GregorianCalendar daquiA2Horas = new GregorianCalendar()
            daquiA2Horas.add(GregorianCalendar.HOUR, 24)//podes setar quanto tempo quiser.
            
            // Usado para formatar a data de acordo com os requisitos do webservice
            SimpleDateFormat sdf = new SimpleDateFormat()
            sdf.applyPattern("yyyy-MM-dd HH:mm:ss")
            
            if(epg.getProgramListByChannelDateInterval(c.getSigla(), sdf.format(new Date()), sdf.format(daquiA2Horas.getTime())) != null){
                def prgrms = epg.getProgramListByChannelDateInterval(c.getSigla(), sdf.format(new Date()), sdf.format(daquiA2Horas.getTime())).getProgram()
                
                //System.out.println("Programação: ");
                
                
                //System.out.println("Canal: "+c.getName()+" Programas: "+prgrms.size())
                prgrms.each { p ->
                    def progammationInstance = new Programmation()
                    /*System.out.println(p.getTitle());
                    System.out.println("Começa: " + p.getStartTime());
                    System.out.println("Acaba: " + p.getEndTime());
                    System.out.println("Descrição: "+p.getDescription());*/
                    progammationInstance.prog_duration = p.getDuration().toString()
                    progammationInstance.prog_title = p.getTitle()
                    progammationInstance.prog_start = new Date().parse("yyyy-MM-dd HH:mm:ss", p.getStartTime())
                    progammationInstance.prog_end = new Date().parse("yyyy-MM-dd HH:mm:ss", p.getEndTime())
                    progammationInstance.prog_description = p.getDescription()
                    progammationInstance.prog_channel = Channel2.findByChannel_name(c.getName())
                    //System.out.println("Passou no programa :"+p.getTitle())
                    
                    if(!Programmation.findWhere(prog_title: p.getTitle(), prog_start: new Date().parse("yyyy-MM-dd HH:mm:ss", p.getStartTime())))
                    progammationInstance.save()                    
                    
                }
            }
        }
        println("success!")
    }
}
